import java.util.Random;

public class CardDrawingSimulator {
    private static final double LEGENDARY_RATE = 0.02; // 传说角色单次邂逅出率
    private static final double UP_LEGENDARY_RATE = 0.5; // 当期UP传说角色占传说角色出率的百分比
    private static final int GLOBAL_GUARANTEE_COUNT = 100; // 全局保底计数
    private static final int FIRST_ENCOUNTER_GUARANTEE_COUNT = 180; // 【初遇】保底计数
    private static final Random RANDOM = new Random();

    public static void main(String[] args) {
        int totalEncounters = 10000000; // 总共模拟的邂逅次数
        int totalLegendaryEncounters = 0; // 总共获得传说角色的次数
        int totalUpLegendaryEncounters = 0; // 总共获得当期UP传说角色的次数
        int currentGlobalGuaranteeCount = 0; // 当前全局保底计数
        int currentFirstEncounterGuaranteeCount = 0; // 当前【初遇】保底计数

        for (int i = 0; i < totalEncounters; i++) {
            // 模拟一次邂逅
            boolean isLegendary = encounterLegendary();

            if (isLegendary) {
                // 如果是传说角色
                totalLegendaryEncounters++;

                if (RANDOM.nextDouble() < UP_LEGENDARY_RATE) {
                    // 如果是当期UP传说角色
                    totalUpLegendaryEncounters++;
                    currentFirstEncounterGuaranteeCount = 0; // 重置【初遇】保底计数
                }

                currentGlobalGuaranteeCount = 0; // 重置全局保底计数
            } else {
                // 如果不是传说角色
                currentGlobalGuaranteeCount++;
                currentFirstEncounterGuaranteeCount++;

                if (currentFirstEncounterGuaranteeCount == FIRST_ENCOUNTER_GUARANTEE_COUNT) {
                    // 触发【初遇】保底
                    totalUpLegendaryEncounters++;
                    currentFirstEncounterGuaranteeCount = 0;
                } else if (currentGlobalGuaranteeCount == GLOBAL_GUARANTEE_COUNT) {
                    // 触发全局保底
                    totalLegendaryEncounters++;
                    currentGlobalGuaranteeCount = 0;
                }
            }
        }

        // 计算概率期望
        double expectedLegendaryEncounters = (double) totalLegendaryEncounters / totalEncounters;
        double expectedUpLegendaryEncounters = (double) totalUpLegendaryEncounters / totalEncounters;

        System.out.println("模拟抽卡次数: " + totalEncounters);
        System.out.println("获得传说角色次数: " + totalLegendaryEncounters);
        System.out.println("获得传说角色期望: " + expectedLegendaryEncounters);
        System.out.println("获得up角色次数: " + totalUpLegendaryEncounters);
        System.out.println("获得up角色期望: " + expectedUpLegendaryEncounters);
    }

    private static boolean encounterLegendary() {
        // 模拟邂逅，根据给定的出率判断是否获得传说角色
        return RANDOM.nextDouble() < LEGENDARY_RATE;
    }
}